﻿using System.Web.Mvc;
using AmazeUI.bll;
using AmazeUI.bll.Open;
using AmazeUI.bll.Open.Weixin;

using Ema.ResNet;

namespace Ema.Controllers
{
    public class OpenController : BaseController
    {
        Wx wx = new Wx();

        protected OpenBll OpenBll = new OpenBll();

        protected XtUserBll XtUserBll = new XtUserBll();
        string StrjsapiTicket = "jsapiTicket";
        string StraccessToken = "accessToken";

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
            OpenBll.Dispose();
            // XtUserBll.Dispose();
        }

        protected Wx GetWx
        {
            get { return wx; }
        }
        /// <summary>
        /// 获取accessToken的JSON
        /// </summary>
        /// <returns></returns>
        [NonAction]
        protected string GetaccessToken()
        {

            if (System.Web.HttpContext.Current.Application[StraccessToken] != null)
            {
                return System.Web.HttpContext.Current.Application[StraccessToken].ToString();
            }
            else
            {
                var accessToken = wx.GetAccess_token();
                this.GetValaccessToken(accessToken);
                var val = wx.GetValAccess_token(accessToken);
                System.Web.HttpContext.Current.Application[StraccessToken] = val;
                return val;
            }
        }

        /// <summary>
        /// 对accessToken原始值操作
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        [NonAction]
        protected virtual void GetValaccessToken(string accessToken)
        {
            // this.OpenBll.SetLog("accessToken", accessToken, 2);
        }

        /// <summary>
        /// 对jsapiTicket原始值操作
        /// </summary>
        /// <param name="jsapiTicket"></param>
        /// <returns></returns>
        [NonAction]
        protected virtual void GetValjsapiTicket(string jsapiTicket)
        {
            // this.OpenBll.SetLog("jsapiTicket", jsapiTicket, 2);
        }

        /// <summary>
        /// 获取jsapiTicket的JSON
        /// </summary>
        /// <returns></returns>
        [NonAction]
        protected string GetjsapiTicket(string accessToken)
        {

            if (System.Web.HttpContext.Current.Application[StrjsapiTicket] != null)
            {
                return System.Web.HttpContext.Current.Application[StrjsapiTicket].ToString();
            }
            else
            {
                var jsapiTicket = wx.Getjsapi_ticket(accessToken);
                var val = wx.GetValJsapi_ticket(jsapiTicket);
                if (val == "")
                {
                    System.Web.HttpContext.Current.Application[StraccessToken] = null;
                    accessToken = this.GetaccessToken();

                    base.Logset("accessToken错误", accessToken);
                    base.Logset("jsapiTicket错误", jsapiTicket);
                }
                else
                {
                    this.GetValjsapiTicket(jsapiTicket);
                    System.Web.HttpContext.Current.Application[StrjsapiTicket] = val;
                }
                return val;
            }
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        [NonAction]
        protected string WxSignature(string url)
        {
            var ga = GetaccessToken();
            return this.wx.GetSignature(url, this.GetjsapiTicket(ga));


        }
        /// <summary>
        /// 获取
        /// </summary>
        protected string Getcodeandstate
        {
            get
            {
                if (System.Web.HttpContext.Current.Application["codeandstate"] == null)
                {
                    return "";
                }
                return System.Web.HttpContext.Current.Application["codeandstate"].ToString();
            }
            set
            {
                System.Web.HttpContext.Current.Application["codeandstate"] = value;
            }
        }

    }

}
